/*=============================================================================
 * File Name: print_log.h
 * Author: WoodWang
 * Date: 2020-03-21
 * Infomation: version 0.01.
 * Copyright (c) 2020-2021,Write by WoodWang.
 *
 * Change Logs:
 * Date           Author       Notes
 
=============================================================================*/
#ifndef __PRINT_LOG_H__
#define __PRINT_LOG_H__

#include <stdio.h>
#include <stdint.h>

//config 
#define CONFIG_DBG_LEVEL        OSAL_DBG_INFO
#define CONFIG_LOG_PRINTF       printf


//=============================================================================
/* DEBUG level */
#define OSAL_DBG_NONE       0
#define OSAL_DBG_ASSERT     1
#define OSAL_DBG_ERROR      1
#define OSAL_DBG_WARNING    2
#define OSAL_DBG_INFO       3
#define OSAL_DBG_DEBUG      4

#ifndef CONFIG_LOG_PRINTF
#define CONFIG_LOG_PRINTF   printf
#endif

#ifndef CONFIG_DBG_LEVEL
#define CONFIG_DBG_LEVEL    OSAL_DBG_INFO
#endif

#ifndef LOG_TAG
#define LOG_TAG    "tag"
#endif

/*
 * The color for terminal (foreground)
 * BLACK    30
 * RED      31
 * GREEN    32
 * YELLOW   33
 * BLUE     34
 * PURPLE   35
 * CYAN     36
 * WHITE    37
 */
#ifdef  CONFIG_LOG_PRINTF_COLOR_ENABLE
//#define _OSAL_DBG_COLOR(n)              CONFIG_LOG_PRINTF("\033[" #n "m")
#define _OSAL_DBG_LOG_HDR(lvl_name, color_n)        \
    CONFIG_LOG_PRINTF("\033[" #color_n "m[" lvl_name "/" LOG_TAG "] ")
#define _OSAL_DBG_LOG_X_END             CONFIG_LOG_PRINTF("\033[0m \r\n")
#else
//#define _OSAL_DBG_COLOR(n)
#define _OSAL_DBG_LOG_HDR(lvl_name, color_n)        \
    CONFIG_LOG_PRINTF("[" lvl_name "/" LOG_TAG "] ")
#define _OSAL_DBG_LOG_X_END CONFIG_LOG_PRINTF("\r\n")
#endif

#define osal_dbg_log_line(lvl, color_n, fmt, ...)   \
    do {                                            \
        _OSAL_DBG_LOG_HDR(lvl, color_n);            \
        CONFIG_LOG_PRINTF(fmt, ##__VA_ARGS__);      \
        _OSAL_DBG_LOG_X_END;                        \
    } while (0)

#if (CONFIG_DBG_LEVEL >= OSAL_DBG_DEBUG)
#define LOG_D(fmt, ...) osal_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
#else
#define LOG_D(...)
#endif

#if (CONFIG_DBG_LEVEL >= OSAL_DBG_INFO)
#define LOG_I(fmt, ...) osal_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
#else
#define LOG_I(...)
#endif

#if (CONFIG_DBG_LEVEL >= OSAL_DBG_WARNING)
#define LOG_W(fmt, ...) osal_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
#else
#define LOG_W(...)
#endif

#if (CONFIG_DBG_LEVEL >= OSAL_DBG_ERROR)
#define LOG_E(fmt, ...) osal_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
#else
#define LOG_E(...)
#endif

#if (CONFIG_DBG_LEVEL >= OSAL_DBG_ASSERT)
#define LOG_A(fmt, ...) osal_dbg_log_line("A", 31, fmt, ##__VA_ARGS__)
#else
#define LOG_A(...)
#endif

#define LOG_ASSERT(f)                           \
    do {                                        \
        if (!(f)){                              \
            CONFIG_LOG_PRINTF("(%s) has assert failed at %s:%u.\n", #f, __FILE__,__LINE__); \
            while(1);   \
        }               \
    } while (0)

#endif  /* __PRINT_LOG_H__ */
